/**
 * @file index.js
 * @author swan
 */
const app = getApp()
import { routerx } from '../../router/rouer'
Page({
    data: {
        indexcity: "郑州",
        indexcitypy: "zhengzhou",
        indexdata: null,
        indexcompanyname: null,
        indexcompanyjs: null,
        indexlogo: null,
        indexhome: null,
        indexline: null,
        indexbranch: null,
        herad: false,
        BarHeight: null,
        dataindex:true,
    },
    fun() {
        this.setData({
            herad: false
        })
        swan.navigateTo({
            url: "../map/map"
        })
    },
    // 将首页获得头部和尾部的数据进行本地存储方便使用
    funset() {
        swan.setStorageSync("name", this.data.indexlogo + "+" + this.data.indexhome)
        swan.setStorage({
            key: "home",
            data: this.data.indexcompanyname + '+' + this.data.indexhome
        })
    },
    fundata(src){
        routerx('/index', { chengshi: this.data.indexcitypy }).then((rel) => {
          if(src){
            clearInterval(src)
          }
            this.setData({
                //  专线的数据
                // indexdata:rel.data.data.zhuanxian,
                // 公司名称
                indexcompanyname: rel.data.data.company.ctoname,
                // 公司介绍
                indexcompanyjs: rel.data.data.company.qyjj,
                // 公司logo
                indexlogo: rel.data.data.logo,
                // 公司手机号
                indexhome: rel.data.data.company.tel,
                // 热门线路推荐
                indexline: rel.data.data.remen,
                // 公司网点
                indexbranch: rel.data.data.wangdian,
                herad: false
            })
    
            this.funset()
            this.indexcity()
        })
    },
    funitem(){
       let src=setInterval(() => {
            routerx('/index', { chengshi: this.data.indexcitypy }).then((rel) => {
                if(src){
                  clearTimeout(src)
                }
                  this.setData({
                      //  专线的数据
                      // indexdata:rel.data.data.zhuanxian,
                      // 公司名称
                      indexcompanyname: rel.data.data.company.ctoname,
                      // 公司介绍
                      indexcompanyjs: rel.data.data.company.qyjj,
                      // 公司logo
                      indexlogo: rel.data.data.logo,
                      // 公司手机号
                      indexhome: rel.data.data.company.tel,
                      // 热门线路推荐
                      indexline: rel.data.data.remen,
                      // 公司网点
                      indexbranch: rel.data.data.wangdian,
                      herad: false,
                      dataindex:false
                  })
                  console.log(this.data.dataindex)
                  this.funset()
                  this.indexcity()
              })
        }, 2000); 
    },
    // 选择地址过长的问题
    indexcity() {
        if (this.data.indexcity.length > 4) {
            var city4 = this.data.indexcity.substring(0, 4)
            this.setData({
                indexcity: city4 + '...'
            })
        }
        if (this.data.indexcompanyname.length > 4) {
            var indexc = this.data.indexcompanyname.substring(0, 4)
            this.setData({
                indexcompanyname: indexc + '...'
            })
        }
    },
    // 监听页面加载的生命周期函数
    onLoad(query) {
        if (query.name) {
            this.setData({
                indexcity: query.name.split(' ')[0],
                indexcitypy: query.name.split(' ')[1],
            })
           this.fundata()
        }
        // this.onLoad()
       this.fundata()
  this.funitem()
    
       
  
        this.setData({
            canIUse: swan.canIUse('button.open-type.getUserInfo')
        });
        this.setData({
            BarHeight: swan.getSystemInfoSync().statusBarHeight*(750/swan.getSystemInfoSync().screenWidth)
        })

    },
  onShow() {
console.log(this.data.BarHeight)
    },
    getUserInfo(e) {
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        });
    }
})
